package com.sda.renren;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.util.Log;
import android.widget.TextView;

public class VoiceActivity extends Activity {
	
	ListTask l_task;
	ProgressDialog dialog;
	Document doc;
	String TAG = "VoiceActivity";
	String tupian;
	String zuozhe=null;
	TextView tp;
	TextView zz;
	
	public void onCreate(Bundle savedInstanceState) {
		setTheme(16974123);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_voice);
		
		tp=(TextView) this.findViewById(R.id.tupian);
		zz=(TextView) this.findViewById(R.id.zuozhe);
		l_task=new ListTask(this);
		l_task.execute("http://voice.meiriyiwen.com/voice/past");
	}
	
	class ListTask extends AsyncTask<String,Integer,String> {

		public ListTask(Context context){
			dialog=ProgressDialog.show(VoiceActivity.this, "", "Loading,Please wait...");
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			
			try{
				doc =Jsoup.parse(new URL("http://voice.meiriyiwen.com/voice/past?page=1"), 5000);
				Log.i(TAG, "connect");
			}catch (MalformedURLException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			Element pic=doc.getElementsByClass("box_list_img").get(0);
			Element author=doc.getElementsByClass("list_author").get(0);
			
			tupian=pic.html();
			zuozhe=author.html();
			Log.e(TAG, ""+tupian);
			Log.e(TAG, ""+zuozhe);
			
			return zuozhe;
		}
		
		protected void onPostExecute(String result){
			
			ImageGetter imgGetter = new Html.ImageGetter() {

				@Override
				public Drawable getDrawable(String source) {
					   InputStream is=null;
					   Log.e(TAG, source);
					   try{
						   is=(InputStream)new URL( "http://voice.meiriyiwen.com"+source).getContent();
						   Drawable drawable = Drawable.createFromStream(is,"src");
						   drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
						   is.close();
						   return drawable;
					   }catch (Exception e) {  
				            return null;  
				        }  
					   
				}
				
			};
			
			
			tp.setText(Html.fromHtml(tupian, imgGetter, null));
			zz.setText(Html.fromHtml(zuozhe));
			dialog.cancel();
		}
	}
}

